package com.example.pet.service.impl;

import com.example.pet.beans.Category;
import com.example.pet.mapper.ICategoryMapper;
import com.example.pet.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liukai
 * @version 1.0.0
 * @ClassName CategoryServiceImpl.java
 * @Description TODO
 * @createTime 2021年04月23日 02:40:00
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Value("${file.img.location}")
    private String fileImgLocation;

    @Autowired
    private ICategoryMapper iCategoryMapper;


    /**
     * 获取所有分类图片
     *
     * @return
     * @throws Exception
     */
    public List<ResponseEntity<byte[]>> getAllCategoryImg() throws Exception {

        List<ResponseEntity<byte[]>> responseEntityList = new ArrayList<>();
        byte[] imageContent = null;
        ResponseEntity<byte[]> responseEntity = null;
        // 获取所有分类信息
        List<Category> categoryList = iCategoryMapper.findAllCategory();
        // 遍历分类列表
        for (Category category : categoryList) {
            String categoryImgPath = fileImgLocation + category.getCategoryImg();
            System.out.println(categoryImgPath);
            imageContent = fileToByte(new File(categoryImgPath));
            final HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);
            responseEntity = new ResponseEntity<>(imageContent, headers, HttpStatus.OK);
            responseEntityList.add(responseEntity);

        }
        return responseEntityList;
    }

    /**
     * 获取所有分类图片
     *
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity<byte[]> getCategoryImg(String imgPath) throws Exception {

        String categoryImgPath = fileImgLocation + imgPath;
        System.out.println(categoryImgPath);
        byte[] imageContent = fileToByte(new File(categoryImgPath));
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<>(imageContent, headers, HttpStatus.OK);

    }

    /**
     * 获取所有分类信息
     *
     * @return
     */
    @Override
    public List<Category> findAllCategory() {
        return iCategoryMapper.findAllCategory();
    }

    /**
     * 文件转换为字节流
     *
     * @param img
     * @return
     * @throws Exception
     */
    public static byte[] fileToByte(File img) throws Exception {
        byte[] bytes = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            BufferedImage bi;
            bi = ImageIO.read(img);
            ImageIO.write(bi, "png", baos);
            bytes = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.close();
        }
        return bytes;
    }

}
